package com.ruoyi.app.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageSerializable;
import com.ruoyi.app.utils.AjaxResult;
import com.ruoyi.app.service.IUserOrderService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.web.wuliu.domain.MaterialMessage;
import com.ruoyi.web.wuliu.domain.UerOrderVo;
import com.ruoyi.web.wuliu.domain.UserOrder;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "小程序端-用户订单")
@RestController
@RequestMapping("/app/userOrder")
public class UserOrderController extends BaseController {

    @Resource
    private IUserOrderService userOrderService;

    @Resource
    private com.ruoyi.web.wuliu.service.IUserOrderService userOrderServiceT;
    /**
     * 找货接口
     */
    @ApiOperation("找货接口")
    @PostMapping("/lookUp")
    public AjaxResult<PageSerializable<MaterialMessage>> lookUp(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<MaterialMessage> list = userOrderService.getOrder();
        return AjaxResult.success(new PageSerializable<>(list));
    }

    /**
     * 找货接口
     */
    @ApiOperation("推荐接口")
    @PostMapping("/recommend")
    public AjaxResult<PageSerializable<MaterialMessage>> recommend(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<MaterialMessage> list = userOrderService.recommend();
        return AjaxResult.success(new PageSerializable<>(list));
    }


    /**
     * 根据ID查看物流详细信息
     */
    @ApiOperation("根据ID查看物流详细信息")
    @GetMapping("/{id}")
    public AjaxResult<MaterialMessage> lookUpById(@PathVariable String id){
        MaterialMessage ma = userOrderService.getOrderById(id);
        return AjaxResult.success(ma);
    }

    /**
     * 我要接单
     */
    @ApiOperation("我要接单")
    @GetMapping("/orderTaking/{id}")
    public AjaxResult orderTaking(@PathVariable String id){
        return userOrderService.orderTaking(id);
    }


    /**
     * 装货订单
     */
    @ApiOperation("订单装货")
    @PostMapping("/loading")
    public AjaxResult loading(@RequestBody UserOrder userOrder){
        return userOrderService.loading(userOrder);
    }


    /**
     * 订单卸货
     */
    @ApiOperation("订单卸货")
    @PostMapping("/unload")
    public AjaxResult unload(@RequestBody UserOrder userOrder){
        return userOrderService.unload(userOrder);
    }


    /**
     * 查看用户订单信息
     */
    @ApiOperation(value = "查看用户订单信息",response =UserOrder.class )
    @PostMapping("/getByUser")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "status",value = "0未中标1中标2已装货3已卸货4完成5撤销订单6接单中",dataType = "String",required = true)
    })
    public AjaxResult<PageSerializable<UerOrderVo>> getByUser(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10")Integer pageSize,
            @RequestParam(required = false) String status){
        PageHelper.startPage(pageNum, pageSize);
        List<UerOrderVo> list = userOrderService.selectByUserId(status);
        return AjaxResult.success(new PageSerializable<>(list));
    }
    @GetMapping("/listByMid/{mid}")
    public TableDataInfo listByMid(@PathVariable String mid)
    {
        startPage();
        UserOrder userOrder = new UserOrder();
        userOrder.setMaterialId(mid);
        List<UserOrder> list = userOrderServiceT.selectUserOrderList(userOrder);
        return getDataTable(list);
    }

    @GetMapping("/designate/{id}")
    public com.ruoyi.common.core.domain.AjaxResult designate(@PathVariable String id)
    {
        UserOrder userOrder = userOrderServiceT.selectUserOrderById(id);
        if (userOrder != null){
            userOrder.setStatus("1");
            userOrderServiceT.updateUserOrder(userOrder);
        }else {
            com.ruoyi.common.core.domain.AjaxResult.error("订单不存在");
        }
        return com.ruoyi.common.core.domain.AjaxResult.success();
    }

}
